#!/usr/bin/env python
# _*_ coding:utf-8 _*_

# Copyright (c) 2020 Huawei Technologies Co., Ltd.
# oec-hardware is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of
# the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF
# ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
# FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2020-11-03

"""the form of admin"""

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, ValidationError
from app.models import Admin


class ManufacturerForm(FlaskForm):
    """添加厂商表单"""
    manufacturer_name = StringField(
        label="厂商名称",
        validators=[
            DataRequired("厂商名称不能为空！"),
        ],
        description="厂商名称",
        render_kw={
            "class": "Sytle_text",
            "placeholder": "请输入商品名称！",
            "size": "50"
        }
    )
    contract_no = StringField(
        label="合同编号",
        validators=[
            DataRequired("合同编号不能为空！"),
        ],
        render_kw={
            "class": "Sytle_text",
            "placeholder": "请输入合同编号！",
            "size": "50"
        }
    )
    submit = SubmitField('保存')


class LoginForm(FlaskForm):
    """
    管理员登录表单
    """
    username = StringField(
        label="管理员名",
        validators=[
            DataRequired("管理员名不能为空")
        ],
        description="管理员名",
        render_kw={
            "class": "manager",
            "placeholder": "请输入管理员名",
        }
    )
    password = PasswordField(
        label="密码",
        validators=[
            DataRequired("密码不能为空")
        ],
        description="密码",
        render_kw={
            "class": "password",
            "placeholder": "请输入密码",
        }
    )
    verify_code = StringField(
        'VerifyCode',
        validators=[DataRequired()],
        render_kw={
            "class": "validate-code",
            "size": 18,
            "maxlength": 4,
        }
    )
    submit = SubmitField(
        label='登录',
        render_kw={
            "class": "btn btn-primary login",
        }
    )

    def validate_manager(self, field):
        """验证账号，命名规则：validate_ + 字段名。如果要验证密码，则可以创建函数validate_pwd"""
        account = field.data
        admin = Admin.query.filter_by(manager=account).count()
        if admin == 0:
            raise ValidationError("账号不存在! ")


class ReviewForm(FlaskForm):
    """用例评审表单"""
    review_result = StringField('result')
    review_comment = StringField('comment')
    submit = SubmitField('添加')


class SearchForm(FlaskForm):
    """管理员进行的厂商、硬件设备等搜索的表单"""
    search_creater = StringField('creater', validators=[DataRequired()])
    submit = SubmitField('搜索')
